<template>
  <div class="scan">
    <div style="background: black;position: fixed;top: 0;left: 0;right: 0;bottom: 0;">
      <div id="bcid"></div>
    </div>
    <div class="btn">
      <div class="btnBox">
        <mu-button class="btns" color="primary" full-width @click="back">取消扫码</mu-button>
      </div>
      <div class="btnBox">
        <mu-button class="btns" color="primary" full-width @click="ctrFlash">{{btnText}}</mu-button>
      </div>
    </div>
  </div>
</template>

<script>
import eventBus from '@/eventListener/listener.js'

var ws=null,wo=null;
var scan=null,domready=false;

export default {
  data () {
    return {
      flashLight: false,
      btnText: "打开闪光灯",
      componentName: ""
    }
  },
  props: ['mark'],
  methods: {
    back () {
      this.$emit('closeing', true)
    },
    ctrFlash () {
      if(!this.flashLight){
        // 打开
        scan.setFlash(true);
        this.btnText = "关闭闪光灯";
      }else{
        // 关闭
        scan.setFlash(false);
        this.btnText = "打开闪光灯";
      }
      this.flashLight = !this.flashLight;
    },
    goback(res){
      //传递一个map，val是key，hospital是value
      // eventBus.$emit('val', res + '&' + this.componentName);//调用router回退页面
      // console.log("res:", res + '&' + this.componentName);
      // scan ? scan.close() : "";
      // this.$router.go(-1);
      this.$emit('childByValue', res + "&" + this.mark)
      this.back();
    },
    startRecognize () {
      var _this = this;
      // H5 plus事件处理
      function plusReady(){
        var filter = [plus.barcode.QR,plus.barcode.CODE128,plus.barcode.CODE39,plus.barcode.DATAMATRIX];
        scan = new plus.barcode.Barcode('bcid', filter);
        scan.onmarked = onmarked;
        scan.start({vibrate: true});
      }
      if(window.plus){
        plusReady();
      }else{
        document.addEventListener('plusready', plusReady, false);
      }
      // 二维码扫描成功
      function onmarked(type, result, file){
          console.log('扫描成功type', type, '返回值result', result);
          result = result.replace(/\r\n/g, '');
          _this.goback(result);
      }
      // 从相册中选择二维码图片 
      function scanPicture(){
          plus.gallery.pick(function(path){
            plus.barcode.scan(path,onmarked,function(error){
            plus.nativeUI.alert('无法识别此图片');
          });
          }, function(err){
              console.log('Failed: '+err.message);
          });
      }
    }
  },
  mounted () {
    setTimeout(() => {
      this.startRecognize();
    }, 100);
  },
  beforeDestroy () {
    if(scan) {
      scan.close();
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.scan{
  background-color: #FAF8F5;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}
#bcid {
  width: 100%;
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 3rem;
  text-align: center;
  color: #fff;
  background: #000;
}
.btn{
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 3rem;
  z-index: 9999;
  display: flex;
  justify-content: space-around;
}
.btn .mu-raised-button.mu-button-full-width{
  height: 100%;
}
.btn .btnBox{
  width: 48%;
  height: 100%;
}
</style>
